package Thread;

import java.util.HashMap;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

class MyThreadPool{
    //自己实现线程池
    //阻塞队列
    BlockingDeque<Runnable> queue = new LinkedBlockingDeque<>();
    public void submit(Runnable runnable) throws InterruptedException {
        //添加到阻塞队列
        queue.put(runnable);

    }
    public MyThreadPool (int p){
        for (int i = 0; i < p; i++) {
            Thread t = new Thread(()->{
                try {
                    //有任务就取
                    while (true){
                        Runnable runnable = queue.take();
                        runnable.run();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            t.start();
        }
    }

}

public class ThreadDemo22  {
    public static void main(String[] args) throws InterruptedException {
        MyThreadPool myThreadPool = new MyThreadPool(10);

        for (int i = 0; i < 1000; i++) {
            int number = i;
            myThreadPool.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println("hello"+number);
                }
            });
        }


    }
}
